<template lang="pug">
.hospitalization-notice
  TemplatePreview(
    style="margin: 0px 0;" mode="form",@load="onLoad" iframeId="hospitalizationNotice"
  )
</template>

<script setup lang="ts">
import { useNotify } from "@scmt/common-components";
import { useRequest } from "@scmt/common-hooks";
import { useApi } from "@scmt/core-api";
//@ts-ignore
import TemplatePreview from "./templtePrviews.vue";
import { useUtils } from "@scmt/lib-utils";
const $utils = useUtils();
//添加 Props属性
const props = defineProps({
  selectItem: Object,
});
//定义事件
const emit = defineEmits(["handleQuery"]);
onMounted(() => {
  request("listAsOptions");
  request("listDoctorAsOptions", "");
});

const $notify = useNotify();
const $api = useApi();
const { request, data } = useRequest({
  patient: (patientId: string) => {
    return $api.patient.queryPatientById(patientId);
  },
  templateData: async (typeId: string) =>
    $api.emr.emrTemplate.getEmrTemplateByEmrTypeId(typeId),
  getspecimenResult: async (specimenId: string) => {
    return await $api.lis.specimen.getPortfolioProjectBySpecimenId(specimenId);
  },
  listAsOptions: () => {
    return $api.lis.specimen.category.listAsOptions();
  },
  listDoctorAsOptions: (val: string) =>
    $api.user.listDoctorAsOptions({ officeId: val }),
});
let editor: any = null;
let doc = ref("");
//加载编辑器
const onLoad = (e: any) => {
  editor = e.target.contentWindow.editor;
  request("templateData", {
    params: "17c2181429eae21c4c45fc8720d83db4",
    onSuccess: (res) => {
      if (res) {
        doc.value = res.content;
        if (editor) {
          let option = {
            // doc: doc.value,
            toolbar: {
              //隐藏工具栏 ：toolbar: false
              file: true, //显示文件
              edit: true, //显示编辑
              insert: true, //显示插入
              expression: true, //显示表达式
              table: true, //显示表格
              revision: true, //显示修订
              view: true, //显示查看
              print: true, //显示打印
              import: true, //显示导入
              export: true, //显示导出
              develop: true, //显示开发工具
              help: true, //显示帮助
            },
            statusbar: true, //是否显示状态栏
            showPageLine: true, //是否显示分页线
            fitWidth: false, //是否适应宽度
            disableCommand: ["template", "save"], //屏蔽文档命令，同时隐藏命令对应工具栏按钮
          };
          //编辑器初始化
          editor.init(option).then(() => {
            //加载html
            editor.loadHtml(doc.value);
            init();
          });
        }
      } else {
        $notify.error("查询模板失败！！");
      }
    },
  });
};

const init = () => {
  if (props.selectItem && props.selectItem.id) {
    let item = Object.assign({}, props.selectItem);
    item &&
      item.id &&
      request("getspecimenResult", item.id).then(() => {
        let specimen = $api.lis.specimen.$$createEntity(
          data.getspecimenResult ?? {}
        );
        if (specimen && specimen.id) {
          item.clinicalDiagnosis = specimen.clinicalDiagnosis; // 诊断意见
          item.name = specimen.patientName;
          item.age = specimen.age;
          item.sex = specimen.sex;
          item.specimen = specimen.specimenNumber;
          item.barcode = specimen.barcode;
          item.checkDate = $utils.getCurrentTimeString();
          item.specimenType = data.listAsOptions?.find(
            (i) => i.value == specimen.specimenTypeId
          )?.label;
          item.barcode = specimen.barcode;

          item.sendInspectionId = data.listDoctorAsOptions?.find(
            (i) => i.value == specimen.sendInspectionId
          )?.label;
          item.inspector = data.listDoctorAsOptions?.find(
            (i) => i.value == specimen.inspector
          )?.label;
          item.reviewer = data.listDoctorAsOptions?.find(
            (i) => i.value == specimen.reviewer
          )?.label;
          item.sendInspectionTime = specimen.sendInspectionTime;
          item.inspectionTime = specimen.inspectionTime;
          item.examineTime = specimen.examineTime;
          let dryChemistry = [
            "LEU",
            "ntt",
            "NIT",
            "URO",
            "BIL",
            "PRO",
            "GLU",
            "SG",
            "BLD",
            "PH",
            "VC",
          ];
          let microscope = [
            "NYYS",
            "TMD",
            "JJHXB",
            "JJBXB",
            "jjtsxb",
            "JJGX",
            "jjbxbgx",
            "jjspxb",
            "JJJJ",
          ];
          const list = [
            {
              NUM: "N0.",
              baseProjectName: "检验结果",
              result: "结果",
              unit: "单位",
              reference: "参考范围",
            },
          ];
          const list1 = [
            {
              monitorName: "干化学项目",
              value: "测量值",
              reference: "参考值",
              monitorName2: "镜检项目",
              value2: "测量值",
              reference2: "参考值",
            },
          ];
          if (specimen.specimenProjectList) {
            for (let i = 0; i < specimen.specimenProjectList.length; i++) {
              list.push({
                NUM: i + 1,
                baseProjectName:
                  specimen.specimenProjectList[i].baseProjectName,
                result: specimen.specimenProjectList[i].result,
                unit: specimen.specimenProjectList[i].unit,
                reference: specimen.specimenProjectList[i].reference,
              });
            }
          }
          // if (specimen.specimenProjectList) {
          //   let dryChemistrys = specimen.specimenProjectList.filter(
          //     (i) => dryChemistry.indexOf(i.shortName) > -1
          //   );
          //   let microscopes = specimen.specimenProjectList.filter(
          //     (i) => microscope.indexOf(i.shortName) > -1
          //   );
          //   let baseProject = [];
          //   if (dryChemistrys && microscopes) {
          //     if (dryChemistry.length > microscope.length) {
          //       baseProject = dryChemistrys;
          //     } else {
          //       baseProject = microscopes;
          //     }
          //   }
          //   if (baseProject.length > 0) {
          //     for (let i = 0; i < baseProject.length; i++) {
          //       let monitorName = "";
          //       let value = "";
          //       let reference = "";
          //       let monitorName2 = "";
          //       let value2 = "";
          //       let reference2 = "";
          //       if (dryChemistrys[i] && dryChemistrys[i].baseProjectName) {
          //         if (dryChemistrys[i].baseProjectName) {
          //           monitorName = dryChemistrys[i].baseProjectName;
          //         }
          //         if (dryChemistrys[i].result) {
          //           value = dryChemistrys[i].result;
          //         }
          //         if (dryChemistrys[i].reference) {
          //           reference = dryChemistrys[i].reference;
          //         }
          //       }
          //       if (microscopes[i] && microscopes[i].baseProjectName) {
          //         if (microscopes[i].baseProjectName) {
          //           monitorName2 = microscopes[i].baseProjectName;
          //         }
          //         if (microscopes[i].result) {
          //           value2 = microscopes[i].result;
          //         }
          //         if (microscopes[i].reference) {
          //           reference2 = microscopes[i].reference;
          //         }
          //       }
          //       list1.push({
          //         monitorName: monitorName,
          //         value: value,
          //         reference: reference,
          //         monitorName2: monitorName2,
          //         value2: value2,
          //         reference2: reference2,
          //       });
          //     }
          //   }
          // }
          if (editor.$("#list") && editor.$("#list").length > 0) {
            editor.bindDataList("list", list);
          }
        }
        if (editor) {
          editor.setBindObject(item);
          editor.execCommand("print");
        }
      });
  }
};

//监听登记单选择改变
watch(
  () => props.selectItem,
  (item) => {
    if (item && item.id) {
      nextTick(() => {
        request("listAsOptions");
        request("listDoctorAsOptions", "");
        init();
      });
    }
  },
  {
    immediate: true,
  }
);
</script>

<style lang="less" scoped>
.hospitalization-notice {
  height: 0px;
}
</style>
